﻿using MslDiary.Utils;
using System;
using System.Collections.Generic;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Threading;

namespace MslDiary.View
{
    /// <summary>
    /// Login.xaml 的交互逻辑
    /// </summary>
    public partial class Login
    {
        public Login()
        {
            InitializeComponent();
            this.pbPwd.PasswordChanged += OnBindedPasswordChanged;
            #region 找回密码

            this.lblSearchDbPwd.Drop += (s, e) =>
            {
                if (!e.Data.GetDataPresent(DataFormats.FileDrop)) //仅支持文件的拖放
                    return;

                string[] files = ((string[])e.Data.GetData(DataFormats.FileDrop));
                string path = String.Empty;
                if (files.Length > 0) path = files[0];
                if (!path.EndsWith(".lic"))
                {
                    Utils.Helper.Instance.AlertWarning("请选择 .lic 文件！");
                    return;
                }
                string account = this.cbxAccount.SelectedItem.ToString();
                if (!this.cbxAccount.Text.Equals(account))
                    account = this.cbxAccount.Text.Trim();

                if (account.Length == 0)
                {
                    Utils.Helper.Instance.AlertWarning("请输入或选择登录名！");
                    return;
                }
                try
                {
                    string msg = MslDiary.Framework.Helper.DecryptDbPwd(account, MSL.Tool.IOHelper.ReadFile(path).Replace("\r\n", ""));
                    Utils.Helper.Instance.AlertSuccess("恭喜您，您的密码是【" + msg + "】，请牢记您的密码！");
                }
                catch (Exception)
                {
                    Utils.Helper.Instance.AlertWarning("请确保您已经输入正确的登录名！");
                }
            };
            #endregion

            //DispatcherTimer timer = new DispatcherTimer();//键盘或鼠标没有操作超过300秒锁定日记本
            //timer.Interval = TimeSpan.FromMilliseconds(10);
            //timer.Tick += (s, e) =>
            //{
            //    if (!MslDiary.Framework.Gloable.LoginingInfo.StartsWith("准备"))
            //        this.lblLoginInfo.Content = MslDiary.Framework.Gloable.LoginingInfo;
            //};
            //timer.Start();
        }
        private void OnBindedPasswordChanged(object sender, System.Windows.RoutedEventArgs e)
        {
            PasswordBox passwordtext = (PasswordBox)sender;
            PasswordBoxHelper.SetPasswordBoxSelection(passwordtext, passwordtext.Password.Length + 1, passwordtext.Password.Length + 1);
        }
    }
}
